package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import entity.DataReplay;
import entity.User;

import service.UserService;
import util.db.DBhelp;
import util.db.RowMapper;

public class DataReplayDao {

	private DBhelp<DataReplay> dBhelp = new DBhelp<DataReplay>();
	
	
	private class DataReplayRowMapper implements RowMapper<DataReplay>{

		public DataReplay mapRow(ResultSet resultSet) throws SQLException {
			DataReplay dataReplay = new DataReplay();
			dataReplay.setId(resultSet.getString("id"));
			dataReplay.setContent(resultSet.getString("content"));
			dataReplay.setCreatetime(resultSet.getString("createtime"));
			dataReplay.setUserid(resultSet.getString("userid"));
			dataReplay.setDataid(resultSet.getString("dataid"));
			return dataReplay;
		}
		
	}


	public void save(DataReplay dataReplay) {
		String sql = "insert into t_datareplay (id,content,createtime,userid,dataid)values(?,?,?,?,?)";
		dBhelp.update(sql, dataReplay.getId(),dataReplay.getContent(),dataReplay.getCreatetime(),dataReplay.getUserid(),dataReplay.getDataid());
	}


	public List<DataReplay> findDataReplayByDataid(String id) {
		String sql = "select * from t_datareplay where dataid=?";
		return dBhelp.executeQueryForList(sql, new RowMapper<DataReplay>() {

			public DataReplay mapRow(ResultSet resultSet) throws SQLException {
				DataReplay dataReplay = new DataReplay();
				dataReplay.setId(resultSet.getString("id"));
				dataReplay.setContent(resultSet.getString("content"));
				dataReplay.setCreatetime(resultSet.getString("createtime"));
				dataReplay.setUserid(resultSet.getString("userid"));
				dataReplay.setDataid(resultSet.getString("dataid"));
				
				User user = new UserService().findUserById(resultSet.getString("userid"));
				dataReplay.setUser(user);
				return dataReplay;
			}
		}, id);
	}


	public void removeByDataid(String dataid) {
		String sql = "delete from t_datareplay where dataid=?";
		dBhelp.update(sql, dataid);
	}


	
}
